package com.tanxin.bill.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tanxin.bill.R;
import com.tanxin.bill.common.base.BaseEvent;
import com.tanxin.bill.common.base.BaseTitleActivity;
import com.tanxin.bill.common.data.BillData;
import com.tanxin.bill.common.data.BillData_;
import com.tanxin.bill.modules.adapter.BillAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;
import io.objectbox.android.AndroidScheduler;
import io.objectbox.query.Query;
import io.objectbox.reactive.DataObserver;

public class BillListActivity extends BaseTitleActivity {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    private Box<BillData> billBox;
    private Query<BillData> billQuery;

    private BillAdapter billAdapter;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, BillListActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int bindLayout() {
        return R.layout.bill_list_activity;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        billBox = MyApp.getApplication().getBoxStore().boxFor(BillData.class);
        billQuery = billBox.query().order(BillData_.id).build();
        billQuery.subscribe(subscriptions).on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<BillData>>() {
                    @Override
                    public void onData(@NonNull List<BillData> data) {
                        billAdapter.setNewData(data);
                    }
                });


        billAdapter = new BillAdapter(null);
        recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle.setAdapter(billAdapter);
        billAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    @Override
    protected void onEventComing(BaseEvent event) {

    }

    @Override
    protected String setTitle() {
        return "账单";
    }


    @OnClick(R.id.btn_add)
    public void onViewClicked() {
       AddBillActivity.startActivity(getContext());
    }
}
